package com.lrs.java.dataStructure.linkList;

/**
 * 按数值大小进行正序排列
 * @author Michael
 *
 */
class SortedList {

	private Link first;
	
	SortedList() {
		first = null;
	}
	
	public void insert(int iData) {
		Link newLink = new Link(iData, iData);
		
		Link current = first;
		Link previous = null;
		while(current!=null && current.getiData()>iData) {
			previous = current;
			current = current.next;
		}
		
		if(previous == null) {
			first = newLink;
		} else {
			previous.next = newLink;
		}
		newLink.next = current;
	}
	
	public Link removeFirst() {
		Link temp = first;
		first = first.next;
		
		return temp;
	}
	
	public void display() {
		Link current = first;
		
		while(current != null) {
			System.out.print("(" + current.getiData() + " " + current.getdData() + ")-->");
			current = current.next;
		}
		System.out.println();
	}
	
}


class SortedListApp {
	
	public static void main(String[] args) {
	      SortedList theSortedList = new SortedList();
	      theSortedList.insert(20);    // insert 2 items
	      theSortedList.insert(40);

	      theSortedList.display(); // display list

	      theSortedList.insert(10);    // insert 3 more items
	      theSortedList.insert(30);
	      theSortedList.insert(50);

	      theSortedList.display(); // display list

	      theSortedList.removeFirst();      // remove an item

	      theSortedList.display(); // display list
	}
}